package com.longai.controller;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.catalina.connector.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import util.Page;

import com.google.gson.Gson;
import com.longai.model.Moment;
import com.longai.model.MomentComment;
import com.longai.model.MomentComment;
import com.longai.model.User;
import com.longai.service.MomentCommentService;
import com.longai.service.MomentService;
import com.longai.service.UserService;
import com.longai.vo.MomentCommentVo;



@Controller
@RequestMapping("momentComment")
public class MomentCommentAction {

	@Autowired
	MomentCommentService momentCommentService;
	
	@Autowired
	UserService userService;
	
	@Autowired
	MomentService momentService;
	//author by miao
	//参数momentId心情Id,currentPage当前页,amount每页显示数目
	//获取心情评论
	//成功返回有分页的momentCommentVo，失败返回""
	@RequestMapping(value = "getComment",method = RequestMethod.GET)
	@ResponseBody
	public Object getComment(@RequestParam("momentId")String momentId,@RequestParam("currentPage")String currentPage
			,@RequestParam("amount")String amount){
			try {
				 Page page = momentCommentService.findByMomentId(momentId,currentPage,amount);  
				    List<MomentComment> momentComments = page.getList();
				    List<MomentCommentVo> comments = new ArrayList<MomentCommentVo>();
				    for(MomentComment m:momentComments){
				    	MomentCommentVo ct = new MomentCommentVo();
				    	ct.setContent(m.getMomentcommentcontent());
				    	ct.setMomentcommentId(m.getMomentcommentId());
				    	ct.setMomentId(m.getMoment().getMomentId());
				    	ct.setNickName(m.getUser().getNickname());
				    	ct.setPubTime(m.getMomentcontentpubtime());
				    	ct.setUserId(m.getUser().getUserId());
				    	comments.add(ct);
				    }
				    page.setList(comments);
					return page;
			} catch (Exception e) {
				return new MomentComment();
			}
		   
	}
	
	//author by miao
	//参数userId评论用户Id,momentId心情Id,comment评论内容
	//添加心情评论
	//成功返回momentCommentVo，失败返回""
	@RequestMapping(value="addMomentComment",method = RequestMethod.GET)
	@ResponseBody
	public Object addMomentComment(@RequestParam("comment")String comment){
		MomentComment momentComment = new MomentComment();
		try {
			Gson gson = new Gson();
			MomentCommentVo momentCommentVo = gson.fromJson(comment, MomentCommentVo.class);
			User user = userService.findById(momentCommentVo.getUserId());
			Moment moment = momentService.findById(momentCommentVo.getMomentId());
			momentComment.setUser(user);
			momentComment.setMomentcommentcontent(momentCommentVo.getContent());
			momentComment.setMoment(moment);
			Date currentTime = new Date();
			Long time = currentTime.getTime();
			momentComment.setMomentcontentpubtime(time);
			momentCommentService.save(momentComment);
			
			return momentCommentVo;
			
		} catch (Exception e) {
			System.out.println(e);
			return "";
		}	
		
	}
	
	//删除某条心情评论
	//author by miao
	//参数momentCommentId心情评论Id
	//成功返回“1”，失败返回“0”
	@RequestMapping(value="deleteMomentComment",method=RequestMethod.GET)
	@ResponseBody
	public Object deleteMomentComment(@RequestParam("momentCommentId")String momentCommentId)
	{
		try {
			MomentComment momentcomment = momentCommentService.findById(Integer.parseInt(momentCommentId));
			momentCommentService.delete(momentcomment);
			return "1";
		} catch (Exception e) {
			System.out.println(e);
			return "0";
		}

		
	}
	
}



